package io.adrainty.bolee.insurance.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.adrainty.bolee.insurance.mapper.SafeguardMapper;
import io.adrainty.bolee.insurance.models.bo.SafeguardBO;
import io.adrainty.bolee.insurance.models.entity.SafeguardEntity;
import io.adrainty.bolee.insurance.repository.ISafeguardRepository;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/10 下午9:47
 */

@Repository
public class SafeguardRepositoryImpl implements ISafeguardRepository {

    @Resource
    private SafeguardMapper safeguardMapper;

    @Override
    public IPage<SafeguardEntity> findSafeguardPage(Integer pageNum, Integer pageSize, SafeguardEntity searchDTO) {
        Page<SafeguardBO> page = new Page<>(pageNum, pageSize);
        Wrapper<SafeguardBO> wrapper = getWrapper(searchDTO);
        Page<SafeguardBO> selectPage = safeguardMapper.selectPage(page, wrapper);
        return selectPage.convert(item -> BeanUtil.toBean(item, SafeguardEntity.class));
    }

    @Override
    public List<SafeguardEntity> findSafeguardList(SafeguardEntity entity) {
        Wrapper<SafeguardBO> wrapper = getWrapper(entity);
        List<SafeguardBO> selectList = safeguardMapper.selectList(wrapper);
        return BeanUtil.copyToList(selectList, SafeguardEntity.class);
    }

    @Override
    public boolean createSafeguard(SafeguardEntity entity) {
        SafeguardBO safeguardBO = BeanUtil.copyProperties(entity, SafeguardBO.class);
        boolean result = safeguardMapper.insert(safeguardBO) > 0;
        if (!result) return false;
        BeanUtil.copyProperties(safeguardBO, entity);
        return true;
    }

    @Override
    public boolean updateSafeguard(SafeguardEntity entity) {
        SafeguardBO safeguardBO = BeanUtil.copyProperties(entity, SafeguardBO.class);
        return safeguardMapper.updateById(safeguardBO) > 0;
    }

    @Override
    public boolean deleteSafeguard(List<String> deleteIdList) {
        if (CollUtil.isEmpty(deleteIdList)) return true;
        return safeguardMapper.deleteByIds(deleteIdList) > 0;
    }

    private static Wrapper<SafeguardBO> getWrapper(SafeguardEntity searchDTO) {
        return Wrappers.lambdaQuery(SafeguardBO.class)
                .eq(StringUtils.isNotBlank(searchDTO.getSafeguardKey()), SafeguardBO::getSafeguardKey, searchDTO.getSafeguardKey())
                .eq(StringUtils.isNotBlank(searchDTO.getSafeguardKeyName()), SafeguardBO::getSafeguardKeyName, searchDTO.getSafeguardKeyName())
                .eq(StringUtils.isNotBlank(searchDTO.getSafeguardVal()), SafeguardBO::getSafeguardVal, searchDTO.getSafeguardVal())
                .eq(searchDTO.getDataState() != null, SafeguardBO::getDataState, searchDTO.getDataState())
                .orderByAsc(SafeguardBO::getSortNo)
                .orderByDesc(SafeguardBO::getCreatedTime);
    }

}
